package com.thermal.thermal_inspection.service.impl;

import com.thermal.thermal_inspection.controller.ClassController;
import com.thermal.thermal_inspection.entity.InspectionDetail;
import com.thermal.thermal_inspection.entity.InspectionGroup;
import com.thermal.thermal_inspection.entity.InspectionGroupDetail;
import com.thermal.thermal_inspection.mapper.InspectionGroupMapper;
import com.thermal.thermal_inspection.service.InspectionGroupService;
import com.thermal.thermal_inspection.vo.AddInspectionDataVO;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class InspectionGroupServiceImpl implements InspectionGroupService {
    @Autowired
    private InspectionGroupMapper inspectionGroupMapper;
    private static final Logger logger = Logger.getLogger(ClassController.class);
    /**
     * 新建数据组
     * @param addInspectionDataVO
     * @return InspectionGroup
     */
    @Override
    public InspectionGroup addInspectionGroup(AddInspectionDataVO addInspectionDataVO) {
        Integer isWrongOfGroup = inspectionGroupMapper.addInspectionGroup(addInspectionDataVO.getInspectionGroupName());
        if (isWrongOfGroup < 1){
            logger.error("插入数据组失败！");
            return null;
        }
        //获取最新的inspectionGroup对象
        InspectionGroup inspectionGroup = inspectionGroupMapper.getInspectionGroupByGroupName(addInspectionDataVO.getInspectionGroupName());
        for (InspectionGroupDetail inspectionGroupDetail : addInspectionDataVO.getGroupDetails()){
            //插入数据组详情
            Integer isWrong = inspectionGroupMapper.addInspectionGroupDetail(inspectionGroup.getInspectionGroupId(),inspectionGroupDetail.getAttribute(),inspectionGroupDetail.getPerUnit());
            if (isWrong < 1){
                logger.error("插入数据组详情失败！");
                return null;
            }
        }
        return inspectionGroup;
    }

    /**
     * 获取巡检数据组列表
     * @return List<InspectionGroup>
     */
    @Override
    public List<InspectionGroup> getInspectionGroups() {
        return inspectionGroupMapper.getInspectionGroups();
    }

    /**
     * 获取巡检数据组详情列表
     * @return List<InspectionGroupDetail>
     */
    @Override
    public List<InspectionGroupDetail> getInspectionGroupDetails(int inspectionGroupId) {
        return inspectionGroupMapper.getInspectionGroupDetails(inspectionGroupId);
    }

    /**
     * 根据名字获取数据组
     * @param inspectionGroupName
     * @return InspectionGroup
     */
    @Override
    public InspectionGroup getInspectionGroupByGroupName(String inspectionGroupName) {
        return inspectionGroupMapper.getInspectionGroupByGroupName(inspectionGroupName);
    }

    /**
     * 新建数据组详情
     * @param inspectionGroupDetail
     * @return Integer
     */
    @Override
    public Integer addInspectionGroupDetail(InspectionGroupDetail inspectionGroupDetail) {
        return inspectionGroupMapper.addInspectionGroupDetail(inspectionGroupDetail.getInspectionGroupId(),inspectionGroupDetail.getAttribute(),inspectionGroupDetail.getPerUnit());
    }

    /**
     * 根据数据组ID获取数据组
     * @param inspectionId
     * @return InspectionGroup
     */
    @Override
    public InspectionGroup getInspectionGroupByInspectionId(int inspectionId) {
        return inspectionGroupMapper.getInspectionGroupByInspectionId(inspectionId);
    }

    /**
     * 修改数据组
     * @param inspectionGroup
     * @return Integer
     */
    @Override
    public Integer updateInspectionGroup(InspectionGroup inspectionGroup) {
        return inspectionGroupMapper.updateInspectionGroupName(inspectionGroup);
    }

    /**
     * 删除数据组
     * @param inspectionGroupId
     * @return Integer
     */
    @Override
    public Integer deleteInspectionGroup(int inspectionGroupId) {
        Integer isWrongGroup = inspectionGroupMapper.deleteInspectionGroup(inspectionGroupId);
        Integer isWrongDetail = inspectionGroupMapper.deleteInspectionGroupDetailByGroupId(inspectionGroupId);
        return (isWrongDetail >= isWrongGroup ? isWrongDetail : isWrongGroup);
    }

    /**
     * 删除数据组详情
     * @param groupDetailId
     * @return Integer
     */
    @Override
    public Integer deleteInspectionGroupDetail(int groupDetailId) {
        return inspectionGroupMapper.deleteInspectionGroupDetailByDetailId(groupDetailId);
    }

}
